package com.chen.blog.controller;

import com.chen.blog.pojo.User;
import com.chen.blog.service.UserService;
import com.chen.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author cgj
 * @date 1/20/25 7:07 PM
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result login(@RequestBody User user){
//      全量数据, 但是前端只需要 用户名 在前台做回显和  uid用于查询自己的文章
        User loginuser = userService.login(user);
        //可以把用到的数据封装为map集合
        if(loginuser!=null){
            HashMap<Object, Object> map = new HashMap<>();
            map.put("uid",loginuser.getUid());
            map.put("username",loginuser.getUsername());
            return Result.ok(map);
        }
        return Result.fail();
    }
    @GetMapping("/{uid}")
    public Result getUserinfoByUid(@PathVariable Integer uid){
        User user = userService.getUserByUid(uid);
        return Result.ok(user);
    }
    @PutMapping()
    public Result changeUserInfo(@RequestBody User user){
        Integer row = userService.changeUserInfo(user);
        if (row>0){
            return  Result.ok();
        }else {
            return  Result.fail();
        }
    }
}
